// <!-- 搜索图标单击事件 -->

window.onload = function () {

    var search = document.getElementById('search');
    var searchList = document.getElementById('search-list');
    var cancle = document.getElementById('cancle-ico');
    var shouye = document.getElementById('shouye');
    var classes = document.getElementById('classes');
    var more = document.getElementById('more');
    var iptSearch = document.querySelector('.ipt-search');

    search.onclick = function () {
        searchList.style.display = 'block';
        this.style.display = 'none';
        shouye.style.display = 'none';
        classes.style.display = 'none';
        more.style.display = 'none';
        iptSearch.focus();
    }
    cancle.onclick = function () {
        searchList.style.display = 'none';
        search.style.display = 'block'
        shouye.style.display = 'block';
        classes.style.display = 'block';
        more.style.display = 'block';
    }
    // 点击search-list外面,list也要消失
    iptSearch.addEventListener('blur', function () {
        searchList.style.display = 'none';
        search.style.display = 'block'
        shouye.style.display = 'block';
        classes.style.display = 'block';
        more.style.display = 'block';
    })


    //<!-- 轮播js -->
    var pre = document.querySelector('.headbanner .pre');
    var next = document.querySelector('.headbanner .next');
    var banner = document.querySelector('.headbanner ul');
    var width = banner.children[0].offsetWidth;
    var num = 0;

    //克隆第一张图放最后
    var cloneFirst = banner.children[0].cloneNode(true);
    banner.appendChild(cloneFirst);
    next.addEventListener('click', function () {
        //无缝滚动
        if (num == banner.children.length - 1) {
            banner.style.left = 0;
            num = 0;
        }
        num++;
        animate(banner, -num * width);
    })

    pre.addEventListener('click', function () {
        if (num == 0) {
            num = banner.children.length - 1;
            banner.style.left = -num * width + 'px';
        }
        num--;
        animate(banner, -num * width);

    })
    //自定播放
    var timer = setInterval(function () {
        next.click(); //手动调用点击事件
    }, 3500)

    banner.addEventListener('mouseenter', function () {
        clearInterval(timer);
    })
    banner.addEventListener('mouseleave', function () {
        timer = setInterval(function () {
            next.click(); //手动调用点击事件
        }, 3500)
    })


 //   <!-- 回到顶部js -->
        var backToTop = document.querySelector('.back-to-top');
        var fenge = document.querySelector('.fenge');
        var fengeTop = fenge.offsetTop;
        var foot = document.querySelector('.foot');
        var footTop = foot.offsetTop;
        
        document.addEventListener('scroll',function(){
            if(window.pageYOffset >= fengeTop){
                backToTop.style.display = 'block';
            }else{
                backToTop.style.display = 'none';
            }
            if(window.pageYOffset+722>= footTop){
                var b = window.pageYOffset + 722 - footTop;
                backToTop.style.bottom = 40 + b +'px';   
            }
        })
}

function animate(obj, target, callback) {  //水平缓动动画(图片需要添加定位)
    clearInterval(obj.timer);
    obj.timer = setInterval(function () {
        var step = (target - obj.offsetLeft) / 10;
        step = step > 0 ? Math.ceil(step) : Math.floor(step);
        if (obj.offsetLeft == target) {
            clearInterval(obj.timer);
        }
        if (callback) {
            callback();
        }
        obj.style.left = obj.offsetLeft + step + 'px';
    },15)  
}  
